package br.com.ss.seguranca.model.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import br.com.ss.seguranca.model.entity.Perfil;
import br.com.ss.seguranca.model.entity.PerfilRotina;

@Component
public class PerfilRotinaDAO {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public void save(PerfilRotina perfilRotina) {
		entityManager.persist(perfilRotina);
	}

	@Transactional
	public void merge(PerfilRotina perfilRotina) {
		entityManager.merge(perfilRotina);
	}

	@Transactional
	public void remove(PerfilRotina perfilRotina) {
		PerfilRotina entity = entityManager.merge(perfilRotina);
		entityManager.remove(entity);
	}

	@SuppressWarnings("unchecked")
	public List<PerfilRotina> list() {
		return entityManager.createQuery("select t from PerfilRotina t")
				.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<PerfilRotina> listPorPerfil(Perfil perfil) {
		Query query = entityManager
				.createQuery("select o from PerfilRotina o " +
						"where o.perfil.id =:perfil_id " );
		query.setParameter("perfil_id", perfil.getId());
		List<PerfilRotina> list = query.getResultList();
		for (PerfilRotina perfilRotina : list) {
			System.out.println(perfilRotina.toString());
			
		}
		return list;
	}

}
